package com.peng.jackson;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.peng.utils.JacksonUtil;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * JacksonReadWriteSample
 *
 * @author lupeng
 * Created on 2022-04-11
 */
public class JacksonReadWriteSample {

    private static final Logger logger = LoggerFactory.getLogger(JacksonReadWriteSample.class);

    private static ObjectMapper objectMapper = new ObjectMapper();

    @NoArgsConstructor
    @AllArgsConstructor
    public static class Bean1 {
        private int id;
        private String name;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    /**
     * 将对象序列化为json
     */
    @Test
    public void s1() throws JsonProcessingException {
        String json = """
                [{"id":11, "name":"peng"},{"id":111, "name":"guo"}]
                """.strip();
        List<Bean1> list = objectMapper.readValue(json, new TypeReference<List<Bean1>>() {});
        System.out.println(list);
    }

    @Test
    public void s2() throws JsonProcessingException {
        String json = """
                {"id":111, "name":"guo"}
                """.strip();
        Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
        });
        System.out.println(map);
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class Bean2 {
        private int id;
        private Name name;

        @Data
        public static class Name {
            private String firstName;
            private String lastName;
        }

        public Name getName() {
            return name;
        }

        public void setName(Name name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

    }

    @Test
    public void s3() throws JsonProcessingException {
        String json = """
                {"id":111, "name":{"firstName":"zhang", "lastName":"san"}}
                """.strip();
        Bean2 bean2 = JacksonUtil.fromJson(json, Bean2.class);
        System.out.println(bean2);
        System.out.println(JacksonUtil.toPrettyJson(bean2));
    }

    @Test
    public void s4() throws JsonProcessingException {
        String json = """
                {"id":111, "name":{"firstName":"zhang", "lastName":"san"}}
                """.strip();
        Bean2 bean2 = JacksonUtil.fromJson(json, TypeFactory.defaultInstance().constructType(Bean2.class));
        System.out.println(bean2);
    }

    @Test
    public void s5() throws JsonProcessingException {
        String json = """
                [{"id":111, "name":{"firstName":"zhang", "lastName":"san"}}, {"id":111, "name":{"firstName":"zhang", "lastName":"san"}}]
                """.strip();
        List<Bean2> bean2s = JacksonUtil.ofJsonCollection(json, List.class, Bean2.class);
        System.out.println(bean2s);
    }

    @Test
    public void s6() throws JsonProcessingException {
        String json = """
                {"id":111, "name": "aaa", "count":11122}
                """.strip();
        Map<String, Object> jsonMap = JacksonUtil.ofJsonMap(json, Map.class, String.class, Object.class);
        System.out.println(jsonMap);
    }

    @Test
    public void s7() throws JsonProcessingException {
        String json = """
                {"id":111, "name": "aaa", "count":11122}
                """.strip();
        Map<String, Object> jsonMap = JacksonUtil.ofJsonMap(json, Map.class, String.class, Object.class);
        System.out.println(jsonMap);
    }

    @Test
    public void s8() throws JsonProcessingException {
        String json = """
                {"id": [1,1,1,12,2,2,2,2]}
                """.strip();
        Map<String, Set<Integer>> setMap = JacksonUtil.ofSetMap(json, Map.class, String.class, Integer.class);
        System.out.println(setMap);
    }

    @Test
    public void s9() throws JsonProcessingException {
        String json = """
                {"name":{"firstName":"aaa", "lastName":"bbb"}, "address":{"addr":"beijing","port":111}}
                """.strip();
        Map<String, Map<String, Object>> mapMap = JacksonUtil.ofMapMap(json, Map.class, String.class, String.class, Object.class);
        System.out.println(mapMap);
    }

}
